package com.babo72.android_test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 조금더 정리 필요하다
 */
public class SmsSend extends Activity {
	
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this.getApplicationContext();
		
		setContentView(R.layout.sms_send);
		this.registerReceiver(new SmsResultReceiver(), new IntentFilter("SMS_SENT"));
		
		Button btn = (Button) this.findViewById(R.id.btnSend);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendSMS();
			}
		});
	}
	
	private void sendSMS() {
		PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SmsSend.class), 0);
		
		Log.d("TAG", "sending SMS...");
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage("01062656356", "from here", "send from android....", pi, null);
	}
	
	class SmsResultReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			int result = this.getResultCode();
			switch(result) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_LONG).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_LONG).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_LONG).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_LONG).show();
					break;
			}
		}
	}
}
